// i.d(t, { l1: () => o, r2: () => c })
import * as THREE from "three"
import s from "./7170"
import Vectors from "../three/Vectors"
import vectorsEnum from "../enum/vectors.enum"
var o = {
  1: "Axes",
  2: "PlanarAxes",
  3: "EdgesAndPlanarAxes",
  4: "Edges",
  5: "Free",
  6: "Locked",
  Axes: 1,
  Edges: 4,
  EdgesAndPlanarAxes: 3,
  Free: 5,
  Locked: 6,
  PlanarAxes: 2
}

const a = {
    [vectorsEnum.UP]: { dir: Object.freeze(Vectors.UP.clone()) },
    [vectorsEnum.DOWN]: { dir: Object.freeze(Vectors.DOWN.clone()) },
    [vectorsEnum.BACK]: { dir: Object.freeze(Vectors.BACK.clone()) },
    [vectorsEnum.LEFT]: { dir: Object.freeze(Vectors.LEFT.clone()) },
    [vectorsEnum.RIGHT]: { dir: Object.freeze(Vectors.RIGHT.clone()) },
    [vectorsEnum.FORWARD]: { dir: Object.freeze(Vectors.FORWARD.clone()) }
  },
  l = {
    [vectorsEnum.HORIZONTAL_PLANE]: {
      dir: Object.freeze(Vectors.HORIZONTAL_PLANE.clone())
    }
  },
  c = (() => {
    let e = 0
    const t = new THREE.Vector3(),
      i = new THREE.Vector3(),
      c = new THREE.Vector3(),
      h = new THREE.Vector3(),
      d = 999,
      u = Object.freeze(Vectors.ZERO.clone()),
      p = Object.keys(a).map(e => Object.assign(Object.assign({}, a[e]), { name: e, angleTo: d })),
      m = Object.keys(l).map(e => Object.assign(Object.assign({}, l[e]), { name: e, angleTo: d })),
      g = e => (e + 3) % 2
    let f = p[0]
    return (n, a, l = o.Axes) => {
      if (l === o.Free)
        return {
          position: a.clone(),
          constrainedAxis: u,
          axisName: vectorsEnum.NONE
        }
      if ((p.forEach(e => (e.angleTo = d)), m.forEach(e => (e.angleTo = d)), c.copy(a).sub(n), (e = c.length()), e < 0.05))
        return {
          position: a.clone(),
          constrainedAxis: u,
          axisName: vectorsEnum.NONE
        }
      h.copy(c).normalize()
      const v = 0.05 * Math.min(e, 1)
      if (l !== o.Locked) {
        for (const e of p) {
          const t = s.ZY(h.angleTo(e.dir))
          t < f.angleTo && (f = e), (e.angleTo = t)
        }
        if (f.angleTo > 20) {
          const e = Math.abs(c.y)
          e * e < v && (f = m[0])
        }
      }
      t.set(n.x * g(f.dir.x), n.y * g(f.dir.y), n.z * g(f.dir.z)), i.set(a.x * Math.abs(f.dir.x), a.y * Math.abs(f.dir.y), a.z * Math.abs(f.dir.z)).add(t)
      return l === o.Locked || i.distanceToSquared(a) < v
        ? {
            position: i.clone(),
            constrainedAxis: f.dir,
            axisName: f.name
          }
        : {
            position: a.clone(),
            constrainedAxis: u,
            axisName: vectorsEnum.NONE
          }
    }
  })()
export default {
  l1: o,
  r2: c
}
